const BASE_URL = 'http://127.0.0.1:8000'  // 修改为实际后端地址

function request(method, url, data = {}) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: BASE_URL + url,
      method,
      data,
      header: { 'Content-Type': 'application/json' },
      success(res) {
        if (res.data.code === 0) resolve(res.data)
        else {
          wx.showToast({ title: res.data.message || '请求错误', icon: 'none' })
          reject(res.data)
        }
      },
      fail(err) {
        wx.showToast({ title: '网络错误', icon: 'none' })
        reject(err)
      }
    })
  })
}

module.exports = {
  get: (url, params)   => request('GET', url, params),
  post:(url, data)     => request('POST', url, data),
}